package au.edu.jcu.it.numberguess;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private NumberGuessApp app;

	Bundle params = new Bundle();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		app = (NumberGuessApp) getApplication();

		ActionBar actionBar = getActionBar();
		actionBar.setHomeButtonEnabled(true);
		actionBar.setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		menu.add(0, 0, 1, "High Scores");

		return true;

	}

	@Override
	protected void onStart() {
		if (app.gameSettings.isOnSound()) {

			if (!app.mediaPlayer.isPlaying()) {

				app.mediaPlayer.start();
			}
		}

		System.out.println("onStart" + app.gameSettings.isOnSound());
		super.onStart();
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_settings:
			Intent intent = new Intent(getApplicationContext(),
					SettingActivity.class);
			startActivity(intent);

			return true;
		case R.id.itemHelp:
			Intent intent1 = new Intent(getApplicationContext(),
					HelpActivity.class);
			startActivity(intent1);

			return true;
		case 0:
			Intent intent2 = new Intent(getApplicationContext(),
					HighScoresActivity.class);
			startActivity(intent2);

			return true;

		}
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void startButtonPressed(View view) {
		app.setAppSecrectNumer();
		EditText etName = (EditText) findViewById(R.id.editTextName);

		app.setUserName(etName.getText().toString());

		Intent intent = new Intent(getApplicationContext(), GameActivity.class);
		startActivity(intent);

	}

	@Override
	protected void onResume() {
		super.onResume();

		if (app.backFromSetting == true) {
			String str = "Saved settings";
			Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
			app.backFromSetting = false;
		}
		if (app.gameSettings.isOnSound()) {
			if (!app.mediaPlayer.isPlaying())
				app.mediaPlayer.start();
		} else {
			if (app.mediaPlayer.isPlaying())
				app.mediaPlayer.pause();
			System.out.println("false1");
		}
	}

	@Override
	public void onBackPressed() {
		
		finish();

		super.onBackPressed();

	}

	@Override
	protected void onDestroy() {
		app.mediaPlayer.stop();
		super.onDestroy();
	}
}
